package com.hq.fxh.No5_User.Jianli.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hq.fxh.CustomerView.PopupWindow.PopupForAddress;
import com.hq.fxh.CustomerView.PopupWindow.PopupForChangeInfo;
import com.hq.fxh.CustomerView.PopupWindow.PopupForHunYinSelect;
import com.hq.fxh.CustomerView.PopupWindow.PopupForList;
import com.hq.fxh.CustomerView.PopupWindow.PopupForSexSelect;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.JianLiUpModle;
import com.hq.fxh.Modle.JianLiWorkModle;
import com.hq.fxh.No5_User.CitySelectActivity;
import com.hq.fxh.No6UerSpace.Fragment.Select;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.PicSelectActivity;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.TimeSelectUtils;
import com.hq.fxh.Utils.Tool;
import com.hq.fxh.Utils.UpLoadUtils;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.data;


/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/21
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_JianLi_XinxiSet extends Fragment implements View.OnClickListener {
    protected View rootView;
    protected ImageView imageJanliSetHeadPic;
    protected TextView tvJianlisetName;
    protected TextView tvJianlisetSex;
    protected TextView tvJianlisetBirth;
    protected TextView tvJianlisetXueli;
    protected TextView tvJianlisetTime;
    protected TextView tvJianlisetPhone;
    protected TextView tvJianlisetEmail;
    protected TextView tvJianlisetAddress;
    protected TextView tvJianlisetHunYin;
    protected TextView tvJianlisetZhuangTai;
    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected LinearLayout toolbarContent;
    PopupForChangeInfo popup;
    int type = 0;
    private TimeSelectUtils timeSelectUtils;
    private PopupForSexSelect popupForSexSelect;
    private PopupForHunYinSelect popupHunYin;
    private PopupForList popupForList;
    private int[] citys = {R.array.def, R.array.北京, R.array.天津, R.array.河北, R.array.山西, R.array.内蒙古
            , R.array.辽宁, R.array.吉林, R.array.黑龙江, R.array.上海, R.array.江苏
            , R.array.浙江, R.array.安徽, R.array.福建, R.array.江西, R.array.山东
            , R.array.河南, R.array.湖北, R.array.湖南, R.array.广东, R.array.广西
            , R.array.海南, R.array.重庆, R.array.四川, R.array.贵州, R.array.云南
            , R.array.西藏, R.array.陕西, R.array.甘肃, R.array.青海, R.array.宁夏
            , R.array.新疆, R.array.台湾, R.array.香港, R.array.澳门};

    String[] province;
    String[] city;
    private PopupForAddress popupPro;
    private PopupForAddress popupCity;
    private String ads = null;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    Bundle data = msg.getData();
                    int position = data.getInt("position");
                    String string = data.getString("string");
                    city = getResources().getStringArray(citys[position]);
                    ads = string;
                    popupCity.setData(city);
                    popupCity.settitle(string);
                    popupCity.show(tvJianlisetAddress);
                    break;
            }
        }
    };
    String picUrlPath = null;
    private JianLiUpModle jianLiModle;
    private PopupForAddress popupXueli;
    private PopupForAddress popupTime;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_jianliset_user, null);
        initView(rootView);
        initData();
        return rootView;
    }


    public void setData(String data) {
        if (data != null) {
            try {
                jianLiModle = new Gson().fromJson(data, JianLiUpModle.class);
            } catch (Exception e) {
                jianLiModle = new JianLiUpModle();
            }
        } else {
            jianLiModle = new JianLiUpModle();
        }

        setViewData();
    }


    private void initData() {

        toolBarTvTitle.setText("教育经历");
        toolBarTvMore.setText("保存");
        if (popup == null) {
            popup = new PopupForChangeInfo(getActivity(), new PopupForChangeInfo.ChangeCallBack() {
                @Override
                public void call(String comment) {
                    if (comment == null || comment.equals("")) {
                        LogUtils.startToash(getContext(), "填写的信息为空");
                    } else {
                        switch (type) {
                            case 0:
                                tvJianlisetName.setText(comment);
                                jianLiModle.setName(comment);
                                break;
                            case 1:
                                tvJianlisetXueli.setText(comment);
                                break;
                            case 2:
                                tvJianlisetTime.setText(comment);

                                break;
                            case 3:
                                tvJianlisetPhone.setText(comment);
                                jianLiModle.setMobile(comment);
                                break;
                            case 4:
                                tvJianlisetEmail.setText(comment);
                                jianLiModle.setEmail(comment);
                                break;


                        }
                    }


                }
            });
        }


        province = getResources().getStringArray(R.array.province);
        popupPro = new PopupForAddress(getActivity(), province, new PopupForAddress.ShareModleCallBack() {
            @Override
            public void callSure(int position, String comment) {
                popupPro.dismiss();

                Message msg = new Message();
                msg.what = 0;
                Bundle data = new Bundle();
                data.putInt("position", position);
                data.putString("string", comment);
                msg.setData(data);

                handler.sendMessage(msg);
                jianLiModle.setProvince_id(position);

            }
        });
        popupCity = new PopupForAddress(getActivity(), city, new PopupForAddress.ShareModleCallBack() {
            @Override
            public void callSure(int position, String comment) {
                LogUtils.e(comment);
                ads = ads + comment;
                tvJianlisetAddress.setText(ads);
                jianLiModle.setCity_id(position);

                popupCity.dismiss();
            }
        });
        popupPro.setTextType(1);

        popupCity.setTextType(1);
    }

    private void setViewData() {

        if (jianLiModle == null) {
            jianLiModle = new JianLiUpModle();
        }

        String header_url = (String) jianLiModle.getHeader_url();
        if (header_url != null) {
            Tool.setUrlToHeadImage(getContext(), header_url, imageJanliSetHeadPic);
        }
        Tool.setStringToText(tvJianlisetName, jianLiModle.getName());
        Tool.setStringToText(tvJianlisetAddress, jianLiModle.getCity_name());
        Tool.setStringToText(tvJianlisetBirth, jianLiModle.getBirthday());
        Tool.setStringToText(tvJianlisetEmail, jianLiModle.getEmail());
        Tool.setStringToText(tvJianlisetPhone, jianLiModle.getMobile());

        if (jianLiModle.getMarry() == 1) {
            Tool.setStringToText(tvJianlisetHunYin, "已婚");

        } else if (jianLiModle.getMarry() == 2) {
            Tool.setStringToText(tvJianlisetHunYin, "未婚");

        } else {
            Tool.setStringToText(tvJianlisetHunYin, "保密");

        }

        if (jianLiModle.getSex() == 1) {
            Tool.setStringToText(tvJianlisetSex, "男");

        } else if (jianLiModle.getSex() == 2) {
            Tool.setStringToText(tvJianlisetSex, "女");

        } else {
            Tool.setStringToText(tvJianlisetSex, "选择性别");

        }

        if (jianLiModle.getEdu() == 1) {
            Tool.setStringToText(tvJianlisetXueli, "中专及以下");

        } else if (jianLiModle.getSex() == 2) {
            Tool.setStringToText(tvJianlisetXueli, "高中");

        } else if (jianLiModle.getSex() == 3) {
            Tool.setStringToText(tvJianlisetXueli, "大专");

        } else if (jianLiModle.getSex() == 4) {
            Tool.setStringToText(tvJianlisetXueli, "本科");

        } else if (jianLiModle.getSex() == 5) {
            Tool.setStringToText(tvJianlisetXueli, "硕士");

        } else if (jianLiModle.getSex() == 6) {
            Tool.setStringToText(tvJianlisetXueli, "博士");

        } else {
            Tool.setStringToText(tvJianlisetXueli, "选择学历");

        }
        if (jianLiModle.getState() == 1) {
            Tool.setStringToText(tvJianlisetZhuangTai, "离职-随时到岗");

        } else if (jianLiModle.getState() == 2) {
            Tool.setStringToText(tvJianlisetZhuangTai, "在职-暂不考虑");

        } else if (jianLiModle.getState() == 3) {
            Tool.setStringToText(tvJianlisetZhuangTai, "在职-考虑机会");

        } else if (jianLiModle.getState() == 4) {
            Tool.setStringToText(tvJianlisetZhuangTai, "在职-月内到岗");

        } else {
            Tool.setStringToText(tvJianlisetZhuangTai, "选择状态");

        }


    }


    private void initView(View rootView) {
        imageJanliSetHeadPic = (ImageView) rootView.findViewById(R.id.image_janliSet_headPic);
        imageJanliSetHeadPic.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetName = (TextView) rootView.findViewById(R.id.tv_jianliset_name);
        tvJianlisetName.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetSex = (TextView) rootView.findViewById(R.id.tv_jianliset_Sex);
        tvJianlisetSex.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetBirth = (TextView) rootView.findViewById(R.id.tv_jianliset_birth);
        tvJianlisetBirth.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetXueli = (TextView) rootView.findViewById(R.id.tv_jianliset_xueli);
        tvJianlisetXueli.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetTime = (TextView) rootView.findViewById(R.id.tv_jianliset_time);
        tvJianlisetTime.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetPhone = (TextView) rootView.findViewById(R.id.tv_jianliset_phone);
        tvJianlisetPhone.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetEmail = (TextView) rootView.findViewById(R.id.tv_jianliset_Email);
        tvJianlisetEmail.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetAddress = (TextView) rootView.findViewById(R.id.tv_jianliset_address);
        tvJianlisetAddress.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetHunYin = (TextView) rootView.findViewById(R.id.tv_jianliset_HunYin);
        tvJianlisetHunYin.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        tvJianlisetZhuangTai = (TextView) rootView.findViewById(R.id.tv_jianliset_ZhuangTai);
        tvJianlisetZhuangTai.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        toolbarImageBack = (ImageView) rootView.findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        toolBarTvTitle = (TextView) rootView.findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) rootView.findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) rootView.findViewById(R.id.toolbar_Layout_More);
        toolbarLayoutMore.setOnClickListener(Fragment_JianLi_XinxiSet.this);
        toolbarContent = (LinearLayout) rootView.findViewById(R.id.toolbar_Content);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.image_janliSet_headPic) {
            choosePic();
        } else if (view.getId() == R.id.tv_jianliset_name) {
            clickName();

        } else if (view.getId() == R.id.tv_jianliset_Sex) {
            clickSex();

        } else if (view.getId() == R.id.tv_jianliset_birth) {
            clickBirth();

        } else if (view.getId() == R.id.tv_jianliset_xueli) {
            clickXueLi();

        } else if (view.getId() == R.id.tv_jianliset_time) {
            clickWorkTime();


        } else if (view.getId() == R.id.tv_jianliset_phone) {
            clickPhone();

        } else if (view.getId() == R.id.tv_jianliset_Email) {
            clickEmail();

        } else if (view.getId() == R.id.tv_jianliset_address) {
            clickAddress();

        } else if (view.getId() == R.id.tv_jianliset_HunYin) {
            clickHunYin();

        } else if (view.getId() == R.id.tv_jianliset_ZhuangTai) {
            clickZhuangTai();

        } else if (view.getId() == R.id.toolbar_Layout_back) {
            getActivity().finish();

        } else if (view.getId() == R.id.toolbar_Layout_More) {
            clicBaoCun();

        }
    }

    private void clicBaoCun() {
        if (jianLiModle == null) {
            LogUtils.e("用户保存错误");

        } else {
            if (jianLiModle.getName() == null) {
                LogUtils.e("用户姓名不能为空");
                return;
            }


        }


        if (jianLiModle != null) {
            int memberId1 = SharedPreferenceUtils.getMemberId(getContext());
            jianLiModle.setMember_id(memberId1);
            String s = new Gson().toJson(jianLiModle);

            LogUtils.e(memberId1 + "  保存的简历信息   " + s);
            UserServiceImpl.upJianLi(s, new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    String string = null;
                    try {
                        ResponseBody body = response.body();
                        if (body == null) {
                            ResponseBody responseBody = response.errorBody();
                            if (responseBody != null) {
                                String string1 = responseBody.string();
                                LogUtils.startToash(getContext(), "保存失败： " + string1);

                            }
                        } else {
                            string = body.string();
                            LogUtils.e(string);
                            LogUtils.startToash(getContext(), "保存成功");
                            getActivity().finish();

                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    LogUtils.e(t.getMessage());

                }
            });
        }

    }

    private void clickZhuangTai() {
        //  当前状态 0未知(默认值)  1离职-随时到岗 2在职-暂不考虑 3在职-考虑机会 4在职-月内到岗
        String[] states = {"离职-随时到岗", "在职-暂不考虑", "在职-考虑机会", "在职-月内到岗"};
        popupForList = new PopupForList(getActivity(), states, new PopupForList.ShareModleCallBack() {
            @Override
            public void callSure(int position, String comment) {
                LogUtils.e(comment);
                tvJianlisetZhuangTai.setText(comment);
                popupForList.dismiss();
                jianLiModle.setState(position + 1);
            }
        });
        popupForList.show(tvJianlisetZhuangTai);
    }

    private void clickHunYin() {
        popupHunYin = new PopupForHunYinSelect(getActivity(), -1, new PopupForHunYinSelect.PopSexCallBack() {
            @Override
            public void callSure(int position, String comment) {
                tvJianlisetHunYin.setText(comment);
                jianLiModle.setMarry(position);
            }
        });
        popupHunYin.show(tvJianlisetHunYin);
    }

    private void clickAddress() {
     /*popupPro.settitle("选择地区");
       popupPro.show(tvJianlisetAddress);*/

        startActivityForResult(new Intent(getContext(), Select.class), 110);
    }

    private void clickEmail() {
        showEdit(tvJianlisetName, 4, AppConstant.请输入邮箱);
    }

    private void clickPhone() {
        showEdit(tvJianlisetName, 3, AppConstant.请输入电话号码);

    }

    private void clickWorkTime() {
        if (popupTime == null) {
            String[] wokeTime = {"应届生", "1年", "2年", "3年", "4年", "5年", "6年", "7年", "8年", "9年", "10年", "十年以上"};
            // String[] wokeTime = {"应届生", "1年", "2年", "3年", "4年", "5年", "5年以上"};

            popupTime = new PopupForAddress(getActivity(), wokeTime, 0, 500, new PopupForAddress.ShareModleCallBack() {
                @Override
                public void callSure(int position, String comment) {
                    popupTime.dismiss();
                    tvJianlisetTime.setText(comment);
                    jianLiModle.setWork(position);

                }
            });
        }

        popupTime.show(tvJianlisetTime);
    }

    private void clickXueLi() {
        if (popupXueli == null) {
            String[] xueli = {"中专及以下", "高中", "大专", "本科", "硕士", "博士"};
            popupXueli = new PopupForAddress(getActivity(), xueli, new PopupForAddress.ShareModleCallBack() {
                @Override
                public void callSure(int position, String comment) {
                    popupXueli.dismiss();
                    tvJianlisetXueli.setText(comment);
                    jianLiModle.setEdu(position + 1);

                }
            });
        }

        popupXueli.show(tvJianlisetXueli);

    }

    private void clickBirth() {
        if (timeSelectUtils == null) {
            timeSelectUtils = new TimeSelectUtils();
        }
        timeSelectUtils.selectTime(getContext(), new TimeSelectUtils.TimeCallBack() {
            @Override
            public void seclectCall(DatePicker view, int year, int month, int dayOfMonth, String time) {
                tvJianlisetBirth.setText(time);
                jianLiModle.setBirthday(time);
            }
        });
    }

    private void clickSex() {

        popupForSexSelect = new PopupForSexSelect(getActivity(), -1, new PopupForSexSelect.PopSexCallBack() {
            @Override
            public void callSure(int position, String comment) {
                tvJianlisetSex.setText(comment);
                jianLiModle.setSex(position);
            }
        });
        popupForSexSelect.show(tvJianlisetSex);

    }

    private void clickName() {
        showEdit(tvJianlisetName, 0, AppConstant.请输入姓名);

    }

    public void showEdit(View view, int type, String title) {
        this.type = type;
        popup.setTitle(title);
        popup.openKeyboard();
        Tool.setAlpha(getActivity(), 0.6f);
        popup.showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    private void choosePic() {
        Intent intent = new Intent(getContext(), PicSelectActivity.class);
        intent.putExtra(AppConstant.maxPic, 1);
        startActivityForResult(intent, 111);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 111 && data != null) {
            String data1 = data.getStringExtra("data");
            if (data1 != null) {
                GlideUtils.loadImageView(getContext(), data1, imageJanliSetHeadPic);
                setUpPic(data1);
            }
        } else if (requestCode == 110 && data != null) {
            int cityID = data.getIntExtra("cityID", -1);
            int proID = data.getIntExtra("proID", -1);
            String cityName = data.getStringExtra("cityName");
            String proName = data.getStringExtra("proName");
            tvJianlisetAddress.setText(cityName);


            if (cityID == -1 || proID == -1) {
                LogUtils.e("城市选择错误");
            } else {
                jianLiModle.setCity_id(cityID);
                jianLiModle.setProvince_id(proID);
                jianLiModle.setCity_name(cityName);
                jianLiModle.setProvince_name(proName);
            }
        }
    }

    public void setUpPic(String picString) {
        UpLoadUtils upLoadUtils = new UpLoadUtils();
        ArrayList<String> picPath = new ArrayList<>();
        picPath.add(picString);
        upLoadUtils.startUpLoadPic(getContext(), picPath, 1, new UpLoadUtils.UpLoadPicCallBack() {
            @Override
            public void onFinishCallBack(ArrayList<String> imageUrl, int tag) {
                if (imageUrl != null) {
                    picUrlPath = imageUrl.get(0);
                    jianLiModle.setHeader_url(picUrlPath);
                } else {
                    LogUtils.startToash(getContext(), "设置头像出错");
                }
            }

            @Override
            public void onErrorCallBack(int position, Throwable e) {
                LogUtils.startToash(getContext(), "设置头像出错 " + e.getMessage());

            }
        });

    }
}
